package com.example.book.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.book.entity.Book;
import com.example.book.entity.Recommend;
import com.example.book.mapper.RecommendMapper;
import com.example.book.service.IBookService;
import com.example.book.service.IFinishService;
import com.example.book.service.IRecommendService;
import com.example.common.dtos.ResponseResult;
import com.example.login.entity.Employee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-10-01
 */
@RestController
@RequestMapping("/book")
@Slf4j
@Api(tags = "图书控制器")
public class BookController {
    @Autowired
    private IBookService bookService;
    @Autowired
    private IRecommendService recommendService;
    @Autowired
    private IFinishService finishService;
    @Autowired
    private RecommendMapper recommendMapper;
    /**
     * 分页回显
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "GET请求调用示例", notes = "图书分页", httpMethod = "GET")
    @GetMapping("/page")
    public ResponseResult<Page> page(Integer page, Integer pageSize, String name) {
        return bookService.pageInfo(page, pageSize, name);
    }

    /**
     * 根据图书ID查询图书信息
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据图书ID查询图书信息")
    @GetMapping("/bookId")
    public ResponseResult bookId(Long[] ids){
        return bookService.bookIdList(ids);
    }


    /**
     * 推荐榜图书详细查询
     * @return
     */
    @ApiOperation(value = "推荐榜图书详细查询")
    @GetMapping("/recommendAll")
    public ResponseResult recommendAll(@RequestParam("name") String name,Integer page, Integer pageSize){
        if (name.equals("推荐榜")){
            return recommendService.recommendAll(page, pageSize);
        }
        if (name.equals("完本榜")){
            return finishService.finishAll();
        }
        return recommendService.recommendAll(page, pageSize);
    }
    /**
     * 修改状态
     *
     * @param
     * @return
     */
    @ApiOperation(value = "修改状态")
    @PutMapping("/status")
    public ResponseResult status(Long id) {
        return ResponseResult.okResult(bookService.updateStatus(id));
    }


    /**
     * 推荐图书
     */
    @ApiOperation(value = "推荐图书")
    @GetMapping("/recommend")
    public ResponseResult recommendBook(Long id){
        Book book = bookService.getById(id);
        if (book.getStatus()){
            Recommend recommend = recommendMapper.selectById(id);
            if (recommend != null){
                return ResponseResult.okResult(500,"图书已经推荐,无法再次推荐!");
            }
            recommendService.addRecommend(book);
            return ResponseResult.okResult("推荐图书成功");
        }
        return ResponseResult.okResult(500,"推荐图书失败");
    }
    /**
     * 删除图书
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除图书")
    @DeleteMapping
    public ResponseResult delete(int[] ids) {
        return bookService.deleteBooks(ids);
    }


}

